﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AugustaDevelopersGuild.Models;
using PagedList;

namespace AugustaDevelopersGuild.Controllers
{
    public class MeetingsController : Controller
    {
        IMeetingRepository meetingRepository;

        private const int PageSize = 25;

        public MeetingsController()
            : this(new MeetingRepository()) {
        }

        public MeetingsController(IMeetingRepository repository)
        {
            meetingRepository = repository;
        }

        public ActionResult Details(int? id)
        {
            Meeting meeting = meetingRepository.Find(id.Value);

            if (meeting == null)
            {
                return View("NotFound");
            }

            return View(meeting);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id)
        {
            Meeting meeting = meetingRepository.Find(id);

            if (meeting == null)
            {
                return View("NotFound");
            }

            return View(meeting);
        }

        [HttpPost, Authorize(Roles = "Admin")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Meeting meeting = meetingRepository.Find(id);

            try
            {
                UpdateModel(meeting);

                meetingRepository.Save();

                return RedirectToAction("Details", new { id = meeting.MeetingID });
            }
            catch
            {
                return View(meeting);
            }
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            Meeting meeting = new Meeting()
            {
                EventDate = DateTime.Now.AddDays(7)
            };

            return View(meeting);
        }

        [HttpPost, Authorize(Roles = "Admin")]
        public ActionResult Create(Meeting meeting)
        {
            if (ModelState.IsValid)
            {
                meetingRepository.InsertOrUpdate(meeting);

                meeting.Rsvps = new List<Rsvp>();
                meeting.Rsvps.Add(new Rsvp { AttendeeName = User.Identity.Name, AttendeeNameId = User.Identity.Name });

                meetingRepository.Save();

                return RedirectToAction("Details", new { id = meeting.MeetingID });
            }

            return View(meeting);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id)
        {
            Meeting meeting = meetingRepository.Find(id);

            if (meeting == null)
            {
                return View("NotFound");
            }

            return View(meeting);
        }

        [HttpPost, Authorize(Roles = "Admin")]
        public ActionResult Delete(int id, string confirmButton)
        {
            Meeting meeting = meetingRepository.Find(id);

            if (meeting == null)
            {
                return View("NotFound");
            }

            meetingRepository.Delete(id);
            meetingRepository.Save();

            return View("Deleted");
        }
    }
}
